package com.jic.market.controller.rest;

import com.jic.common.base.vo.RestResult;
import com.jic.market.api.rest.AllocationRestApi;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.request.rest.allocation.QueryAllocationAndThemeInfoRequest;
import com.jic.market.request.rest.allocation.UpdateAllocationInfoRequest;
import com.jic.market.response.rest.allocation.QueryAllocationAndThemeInfoResponse;
import com.jic.market.service.rest.allocation.AllocationRestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xuyongliang
 * @version V1.0
 * @className AllocationRestController
 * @description TODO
 * @date 2020/4/10
 */
@RestController
@RequestMapping("/rest/allocation")
@Api(tags = {"分摊H5接口"})
@Slf4j
public class AllocationRestController implements AllocationRestApi {

    @Resource
    private AllocationRestService allocationRestService;

    @PostMapping("/query-allocation-and-theme-info")
    @ApiOperation(value = "根据活动编码，查询分摊信息以及所对应的活动信息")
    @Override
    public RestResult<List<QueryAllocationAndThemeInfoResponse>> queryAllocationAndThemeInfo(
            @RequestBody QueryAllocationAndThemeInfoRequest request) {
        RestResult<List<QueryAllocationAndThemeInfoResponse>> restResult = null;
        try{
            log.info("-----进入分摊H5接口--根据活动编码,查询分摊信息以及所对应的活动信息--参数{}-----",request);
            restResult =
                    allocationRestService.queryAllocationAndThemeInfo(request);
            log.info("-----进入分摊H5接口--根据活动编码,查询分摊信息以及所对应的活动信息--出参{}-----",restResult);
        }catch (Exception e){
            log.info("-----进入分摊H5接口--根据活动编码,查询分摊信息以及所对应的活动信息--异常{}-----",e);
            restResult = RestResult.error(MarketErrorCodeEnum.SYS_ERROR.getCode(), e.getMessage());
        }
        return restResult;
    }

    @PostMapping("/update-allocation-info")
    @ApiOperation(value = "根据活动编码，更新分摊信息")
    @Override
    public RestResult<Boolean> updateAllocationInfo(@RequestBody UpdateAllocationInfoRequest request) {
        RestResult<Boolean> restResult = null;
        try{
            log.info("-----进入分摊H5接口--根据活动编码,更新分摊信息--参数{}-----",request);
            restResult =
                    allocationRestService.updateAllocationInfo(request);
            log.info("-----进入分摊H5接口--根据活动编码,更新分摊信息--出参{}-----",restResult);
        }catch (Exception e){
            log.info("-----进入分摊H5接口--根据活动编码,更新分摊信息--异常{}-----",e);
            restResult = RestResult.error(MarketErrorCodeEnum.SYS_ERROR.getCode(), e.getMessage());
        }
        return restResult;
    }
}
